<?php
Yii::import('application.components.Reference');
class BiayaSalesController extends GxController
{
    public function actionCreate()
    {
        if (!Yii::app()->request->isAjaxRequest) return;
        if (isset($_POST) && !empty($_POST)) {
            Yii::import('application.components.Gl');
            $gl = new Gl();
            $is_new = $_POST['mode'] == 0;
            $biaya_sales_id = $_POST['id'];
            $msg = 'Biaya sales berhasil disimpan.';
            Yii::import('application.components.U');
            $detils = CJSON::decode($_POST['detil']);
            app()->db->autoCommit = false;
            $transaction = Yii::app()->db->beginTransaction();
            try {
                $biaya_sales = $is_new ? new BiayaSales : $this->loadModel($biaya_sales_id,
                    "BiayaSales");
                $docref = $biaya_sales->doc_ref;
                if ($is_new) {
                    $ref = new Reference();
                    $docref = $ref->get_next_reference(BIAYASALES);
                }
                foreach ($_POST as $k => $v) {
                    if (is_angka($v)) $v = get_number($v);
                    $_POST['BiayaSales'][$k] = $v;
                }
                $_POST['BiayaSales']['doc_ref'] = $docref;
                $biaya_sales->attributes = $_POST['BiayaSales'];
                if (!$biaya_sales->save())
                    throw new Exception("Gagal menyimpan biaya sales. " . CHtml::errorSummary($biaya_sales));
                BiayaSalesLain::model()->deleteAll('biaya_sales_id  = :biaya_sales_id',
                    array(':biaya_sales_id' => $biaya_sales_id));
                foreach ($detils as $detil) {
                    $biaya_sales_lain = new BiayaSalesLain;
                    $_POST['BiayaSalesLain']['account_code'] = $detil['account_code'];
                    $_POST['BiayaSalesLain']['nama_biaya'] = $detil['nama_biaya'];
                    $_POST['BiayaSalesLain']['amount'] = get_number($detil['amount']);
                    $_POST['BiayaSalesLain']['biaya_sales_id'] = $biaya_sales->biaya_sales_id;
                    $biaya_sales_lain->attributes = $_POST['BiayaSalesLain'];
                    if (!$biaya_sales_lain->save())
                        throw new Exception("Gagal menyimpan biaya sales lain-lain." . CHtml::errorSummary($biaya_sales_lain));
                    $gl->add_gl(BIAYASALES, $biaya_sales->biaya_sales_id, $biaya_sales->tgl, $biaya_sales->doc_ref,
                        $biaya_sales_lain->account_code, 'Beban Lain', '', $biaya_sales_lain->amount);
                }
                if ($is_new) {
                    $ref->save(BIAYASALES, $biaya_sales->biaya_sales_id, $docref);
                }
                if ($biaya_sales->makan != 0) {
                    $gl->add_gl(BIAYASALES, $biaya_sales->biaya_sales_id, $biaya_sales->tgl, $biaya_sales->doc_ref,
                        BEBANKELILINGAN, 'Beban Kelilingan', '', $biaya_sales->makan);
                }
                if (($biaya_sales->bbm + $biaya_sales->parkir) != 0) {
                    $gl->add_gl(BIAYASALES, $biaya_sales->biaya_sales_id, $biaya_sales->tgl, $biaya_sales->doc_ref,
                        BEBANTRANSPORTASI, 'Beban Transportasi', '', ($biaya_sales->bbm + $biaya_sales->parkir));
                }
                if ($biaya_sales->total != 0) {
                    $gl->add_gl(BIAYASALES, $biaya_sales->biaya_sales_id, $biaya_sales->tgl, $biaya_sales->doc_ref,
                        KASSEMENTARA, '', '', -$biaya_sales->total);
                }
                $gl->validate();
                $transaction->commit();
                $status = true;
            } catch (Exception $ex) {
                $transaction->rollback();
                $status = false;
                $msg = $ex->getMessage();
            }
            echo CJSON::encode(array(
                'success' => $status,
                'msg' => $msg));
            Yii::app()->end();
        }
    }
    public function actionUpdate($id)
    {
        $model = $this->loadModel($id, 'BiayaSales');
        if (isset($_POST) && !empty($_POST)) {
            foreach ($_POST as $k => $v) {
                if (is_angka($v)) $v = get_number($v);
                $_POST['BiayaSales'][$k] = $v;
            }
            $msg = "Data gagal disimpan";
            $model->attributes = $_POST['BiayaSales'];
            if ($model->save()) {
                $status = true;
                $msg = "Data berhasil di simpan dengan id " . $model->biaya_sales_id;
            } else {
                $msg .= " " . CHtml::errorSummary($model);
                $status = false;
            }
            if (Yii::app()->request->isAjaxRequest) {
                echo CJSON::encode(array(
                    'success' => $status,
                    'msg' => $msg
                ));
                Yii::app()->end();
            } else {
                $this->redirect(array('view', 'id' => $model->biaya_sales_id));
            }
        }
    }
    public function actionDelete($id)
    {
        if (Yii::app()->request->isPostRequest) {
            $msg = 'Data berhasil dihapus.';
            $status = true;
            try {
                $this->loadModel($id, 'BiayaSales')->delete();
            } catch (Exception $ex) {
                $status = false;
                $msg = $ex->getMessage();
            }
            echo CJSON::encode(array(
                'success' => $status,
                'msg' => $msg));
            Yii::app()->end();
        } else
            throw new CHttpException(400,
                Yii::t('app', 'Invalid request. Please do not repeat this request again.'));
    }
    public function actionIndex()
    {
        $tgl = $_POST['tgl'];
        $sales = $_POST['sales'];
        $criteria = new CDbCriteria();
        $criteria->addCondition("salesman_id = :salesman_id");
        $criteria->addCondition("tgl = :tgl");
        $criteria->params = array(':salesman_id' => $sales, ':tgl' => $tgl);
        $model = BiayaSales::model()->findAll($criteria);
        $total = BiayaSales::model()->count($criteria);
        $this->renderJson($model, $total);
    }
}